module executor.friend.DeleteFriend;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import std.array;
import std.conv : to;
import hunt.util.Serialize;
import client.GateServerClient;
import client.MsgServerClient;
import service.ContactsService;
import service.ApplyService;
import service.FriendService;

@TransportServer(LogicServerName)
class DeleteFriend : AbstractExecutor!(DeleteFriend){

    //删除好友接口
    @MessageId(MESSAGE.CMD_DELETE_FRIEND_REQ)
    void deleteFriend(TransportContext ctx, MessageBuffer buffer)
    {
        auto req = new DeleteContactREQ();
        buffer.data.fromProtobuf!DeleteContactREQ(req);
        // TODO: 这里不应该用传送过来的 userId
        logInfof("Delete contact, userId: %s, contactId: %s", req.userId, req.friendId);
        FriendService.processDeleteFriend(req.userId, req.friendId);
        ApplyService.updateApplyContactReq(req.chatId, 3);

        string userId = req.userId;
        string friendId = req.friendId;
        TransportSession msgClient = null;

        ChatMapAction chatMapAction = new ChatMapAction;
        chatMapAction.chatId = req.chatId;
        chatMapAction.type = ChatMapActionType.DESTROY;
        msgClient = MsgServerClient.getInstance().getForwardClient(to!int(req.chatId));
        msgClient.send(new MessageBuffer(MESSAGE.CMD_CHAT_MAP_ACTION, chatMapAction.toProtobuf.array)); //根msgserver同步chatId下的成员列表

        msgClient = MsgServerClient.getInstance().getForwardClient(to!int(userId));
        req.userId = userId;
        req.friendId = friendId;
        msgClient.send(new MessageBuffer(MESSAGE.CMD_DELETE_FRIEND_REQ, req.toProtobuf.array)); //通过msgserver把删除好友的回调返回给双方
    }
}
